package com.designpattern.softwaredesignpatternstudy.pattern.factory;

import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import javax.swing.*;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import java.awt.*;
import java.io.File;

/**
 * @Author: Jiangxx
 * @Date: 2021/10/08
 * @Description: 抽象工厂方法实例
 */
public class FarmTest {
    public static void main(String[] args) {
        try {
            Farm f;
            Animals a;
            Plant p;
            f = (Farm) ReadXML.getObject();
            a = f.newAnimal();
            p = f.newPlant();
            a.show();
            p.show();
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }
}
interface Animals {
    void show();
}

class A_Horse implements Animals {

    JScrollPane sp;
    JFrame jf = new JFrame("抽象工厂模式测试");
    public A_Horse() {
        Container contentPane = jf.getContentPane();
        JPanel p1 = new JPanel();
        p1.setLayout(new GridLayout(1, 1));
        p1.setBorder(BorderFactory.createTitledBorder("动物：马"));
        sp = new JScrollPane(p1);
        contentPane.add(sp, BorderLayout.CENTER);
        JLabel l1 = new JLabel(new ImageIcon("src/A_Horse.jpg"));
        p1.add(l1);
        jf.pack();
        jf.setVisible(false);
        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        //用户点击窗口关闭
    }
    @Override
    public void show() {
        jf.setVisible(true);
    }
}

class A_Cattle implements Animals {

    JScrollPane sp;
    JFrame jf = new JFrame("抽象工厂模式测试");
    public A_Cattle() {
        Container contentPane = jf.getContentPane();
        JPanel p1 = new JPanel();
        p1.setLayout(new GridLayout(1, 1));
        p1.setBorder(BorderFactory.createTitledBorder("动物：牛"));
        sp = new JScrollPane(p1);
        contentPane.add(sp, BorderLayout.CENTER);
        JLabel l1 = new JLabel(new ImageIcon("src/A_Cattle.jpg"));
        p1.add(l1);
        jf.pack();
        jf.setVisible(false);
        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//用户点击窗口关闭
    }
    @Override
    public void show() {
        jf.setVisible(true);
    }
}

interface Plant {
    void show();
}

class P_Fruitage implements Plant {

    JScrollPane sp;
    JFrame jf = new JFrame("抽象工厂模式测试");
    public P_Fruitage() {
        Container contentPane = jf.getContentPane();
        JPanel p1 = new JPanel();
        p1.setLayout(new GridLayout(1, 1));
        p1.setBorder(BorderFactory.createTitledBorder("植物：水果"));
        sp = new JScrollPane(p1);
        contentPane.add(sp, BorderLayout.CENTER);
        JLabel l1 = new JLabel(new ImageIcon("src/P_Fruitage.jpg"));
        p1.add(l1);
        jf.pack();
        jf.setVisible(false);
        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//用户点击窗口关闭
    }
    @Override
    public void show() {
        jf.setVisible(true);
    }
}

class P_Vegetables implements Plant {

    JScrollPane sp;
    JFrame jf = new JFrame("抽象工厂模式测试");
    public P_Vegetables() {
        Container contentPane = jf.getContentPane();
        JPanel p1 = new JPanel();
        p1.setLayout(new GridLayout(1, 1));
        p1.setBorder(BorderFactory.createTitledBorder("植物：蔬菜"));
        sp = new JScrollPane(p1);
        contentPane.add(sp, BorderLayout.CENTER);
        JLabel l1 = new JLabel(new ImageIcon("src/P_Vegetables.jpg"));
        p1.add(l1);
        jf.pack();
        jf.setVisible(false);
        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//用户点击窗口关闭
    }
    @Override
    public void show() {
        jf.setVisible(true);
    }
}
//抽象工厂：农场类
interface Farm {
    Animals newAnimal();
    Plant newPlant();
}
//具体工厂：韶关农场类
class SGfarm implements Farm {
    @Override
    public Animals newAnimal() {
        System.out.println("新牛出生！");
        return new A_Cattle();
    }
    @Override
    public Plant newPlant() {
        System.out.println("蔬菜长成！");
        return new P_Vegetables();
    }
}
//具体工厂：上饶农场类
class SRfarm implements Farm {
    @Override
    public Animals newAnimal() {
        System.out.println("新马出生！");
        return new A_Horse();
    }
    @Override
    public Plant newPlant() {
        System.out.println("水果长成！");
        return new P_Fruitage();
    }
}
class ReadXML {
    public static Object getObject() {
        try {
            DocumentBuilderFactory dFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder builder = dFactory.newDocumentBuilder();
            Document doc;
            doc = builder.parse(new File("src/config.xml"));
            NodeList nl = doc.getElementsByTagName("className");
            Node classNode = nl.item(0).getFirstChild();
            String cName = "com.designpattern.softwaredesignpatternstudy.pattern.factory." + classNode.getNodeValue();
            System.out.println("新类名：" + cName);
            Class<?> c = Class.forName(cName);
            Object obj = c.newInstance();
            return obj;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}